package com.qyhl.webtv.commonlib.item.fuquan;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.GlobalNewsBean;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

public class ItemFuQuanUnion implements ItemViewDelegate<GlobalNewsBean> {
    private Context context;

    public ItemFuQuanUnion(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_fuquan_union;
    }

    @Override
    public boolean isForViewType(GlobalNewsBean item, int position) {
        return ("11".equals(item.getType()) || "12".equals(item.getType()) || "13".equals(item.getType())) && item.getNewsStyle() != 2;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ViewHolder holder, GlobalNewsBean bean, int position) {
        RoundedImageView cover = holder.getView(R.id.cover);

        Glide.with(context)
                .load(bean.getCover())
                .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                .into(cover);

        holder.setText(R.id.title, bean.getTitle());

        //来源
        TextView origin = holder.getView(R.id.origin);
        switch (bean.getType()) {
            case "11":
                origin.setText("镇街");
                break;
            case "12":
                origin.setText("行局");
                break;
            case "13":
                origin.setText("专题");
                break;
            default:
                origin.setText("未知");
                break;
        }

        //阅读数
        TextView scan = holder.getView(R.id.scan);
        try {
            int minPlay = Integer.parseInt(CommonUtils.getInstance().getMinPlay());
            if (minPlay == 0) {
                scan.setVisibility(View.GONE);
            } else {
                //判断评论数是否大于最少浏览数
                if (StringUtils.isEmpty(bean.getScanNum() + "")) {
                    scan.setVisibility(View.GONE);
                } else if (bean.getScanNum() >=
                        minPlay) {
                    scan.setText(StringUtils.transformNum(bean.getScanNum() + "") + "阅");
                    scan.setVisibility(View.VISIBLE);
                } else {
                    scan.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
            scan.setVisibility(View.GONE);
        }
    }
}
